package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.vo.shopping.AllProductVo;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IContentService;
import com.mall.shopping.IHomeService;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: cs-mall_35th
 * @description: 商品模块之商品
 * @author: Weiwei Liu
 * @create: 2021-12-11 19:29
 **/

@RestController
@RequestMapping("/shopping")
public class GoodsController {

    @Reference(timeout = 3000, retries = 0, check = false)
    IProductService productService;
    @Reference(timeout = 3000, retries = 0, check = false)
    IHomeService homeService;
    @Reference(timeout = 3000, retries = 0, check = false)
    IContentService contentService;
    @Reference(timeout = 3000, retries = 0, check = false)
    IProductCateService productCateService;


    /**
     * 主页显示
     * 数据库连接失败时,显示连接超时错误
     *
     * @return
     */
    @Anoymous
    @GetMapping("homepage")
    public ResponseData homepage() {
        HomePageResponse response = homeService.homepage();
        if (ShoppingRetCode.SYSTEM_TIMEOUT.getCode().equals(response.getCode())) {
            return new ResponseUtil<>().setErrorMsg("连接超时");
        }
        return new ResponseUtil<>().setData(response.getPanelContentItemDtos());
    }


    /**
     * 导航栏显示
     * 数据库连接失败时,显示连接超时错误
     *
     * @return
     */
    @Anoymous
    @GetMapping("navigation")
    public ResponseData navigation() {
        NavListResponse navListResponse = contentService.queryNavList();
        if (ShoppingRetCode.SYSTEM_TIMEOUT.getCode().equals(navListResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg("连接超时");
        }
        return new ResponseUtil<>().setData(navListResponse.getPannelContentDtos());
    }


    /**
     * 列举所有商品种类
     * 数据库连接失败时,显示连接超时错误
     *
     * @return
     */
    @Anoymous
    @GetMapping("categories")
    public ResponseData categories() {
        AllProductCateResponse allProductCate = productCateService.getAllProductCate();
        if (ShoppingRetCode.SYSTEM_TIMEOUT.getCode().equals(allProductCate.getCode())) {
            return new ResponseUtil<>().setErrorMsg("连接超时");
        }
        return new ResponseUtil<>().setData(allProductCate.getProductCateDtoList());
    }


    // 查看商品详情

    /**
     * 参数校验失败,显示参数异常错误信息
     * 连接超时,显示连接超时异常
     * @param id
     * @return
     */
    @Anoymous
    @GetMapping("product/{id}")
    public ResponseData product(@PathVariable("id") Long id) {
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);
        ProductDetailResponse productDetail = productService.getProductDetail(productDetailRequest);
        if (ShoppingRetCode.PARAMETER_VALIDATION_FAILED.getCode().equals(productDetail.getCode())) {
            return new ResponseUtil<>().setErrorMsg(400, "参数异常");
        } else if (ShoppingRetCode.SYSTEM_TIMEOUT.getCode().equals(productDetail.getCode())) {
            return new ResponseUtil<>().setErrorMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());

        }

        return new ResponseUtil<>().setData(productDetail.getProductDetailDto());
    }


    // 分页查询商品列表
    @Anoymous
    @GetMapping("goods")
    public ResponseData goods(AllProductRequest allProductRequest, HttpServletRequest request) {

        AllProductResponse allProductResponse = productService.getAllProduct(allProductRequest);
        List<ProductDto> productDtoList = allProductResponse.getProductDtoList();
        Long total = allProductResponse.getTotal();

        AllProductVo<ProductDto> allProductVo = new AllProductVo<>();
        allProductVo.setData(productDtoList);
        allProductVo.setTotal(total);


        if (allProductResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(allProductVo);

        }
        return new ResponseUtil<>().setErrorMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
    }


    // 查询推荐商品
    @Anoymous
    @GetMapping("recommend")
    public ResponseData recommend(HttpServletRequest request) {

        RecommendResponse recommendResponse = productService.getRecommendGoods();

        if (recommendResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(recommendResponse.getPanelContentItemDtos());
        }
        return new ResponseUtil<>().setErrorMsg(ShoppingRetCode.SYSTEM_ERROR.getCode());
    }
}
